#!/usr/bin/env python3
"""
修复剩余的Swagger注解问题
"""
import os
import re

def fix_swagger_issues(file_path):
    """修复文件中剩余的Swagger注解问题"""
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
        
        original_content = content
        
        # 修复@ApiModel注解
        content = re.sub(r'@ApiModel\s*\([^)]*\)', '@Schema', content)
        
        # 修复@ApiModelProperty注解
        content = re.sub(r'@ApiModelProperty\s*\([^)]*\)', '@Schema', content)
        
        # 修复@ApiParam注解
        content = re.sub(r'@ApiParam\s*\([^)]*\)', '@Parameter', content)
        
        # 添加必要的导入
        if '@Schema' in content and 'import io.swagger.v3.oas.annotations.media.Schema;' not in content:
            # 在package声明后添加Schema导入
            content = re.sub(
                r'(package [^;]+;)\s*\n',
                r'\1\n\nimport io.swagger.v3.oas.annotations.media.Schema;\n',
                content
            )
        
        if '@Parameter' in content and 'import io.swagger.v3.oas.annotations.Parameter;' not in content:
            # 在package声明后添加Parameter导入
            content = re.sub(
                r'(package [^;]+;)\s*\n',
                r'\1\n\nimport io.swagger.v3.oas.annotations.Parameter;\n',
                content
            )
        
        # 移除重复的导入
        lines = content.split('\n')
        seen_imports = set()
        cleaned_lines = []
        
        for line in lines:
            if line.strip().startswith('import '):
                if line.strip() not in seen_imports:
                    seen_imports.add(line.strip())
                    cleaned_lines.append(line)
            else:
                cleaned_lines.append(line)
        
        content = '\n'.join(cleaned_lines)
        
        # 如果有更改，写回文件
        if content != original_content:
            with open(file_path, 'w', encoding='utf-8') as f:
                f.write(content)
            print(f"✓ 修复: {file_path}")
            return True
        else:
            return False
            
    except Exception as e:
        print(f"✗ 错误处理文件 {file_path}: {e}")
        return False

def main():
    """主函数"""
    # 需要修复的文件列表
    problem_files = [
        "src/main/java/com/itjin/model/domain/dto/ResetPasswordDTO.java",
        "src/main/java/com/itjin/model/domain/vo/PostCommentVO.java", 
        "src/main/java/com/itjin/model/domain/vo/PostVO.java",
        "src/main/java/com/itjin/controller/FileController.java"
    ]
    
    print("开始修复剩余的Swagger注解问题...")
    print("-" * 50)
    
    updated_files = 0
    for file_path in problem_files:
        if os.path.exists(file_path):
            if fix_swagger_issues(file_path):
                updated_files += 1
        else:
            print(f"⚠ 文件不存在: {file_path}")
    
    print("-" * 50)
    print(f"修复完成! 更新了 {updated_files} 个文件")

if __name__ == "__main__":
    main() 